import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.1
import QtQml.Models 2.2

Item {
    property alias visualModel: visualModelId
    property ListView listView

    DelegateModel {
            id: visualModelId
            delegate: dragDelegate
        }

    Component {
            id: dragDelegate

                Rectangle {
                    id: content
                    width: parent.width
                    height: rowId.height


                    states: State {
                        when: dragArea.held

                        ParentChange { target: content; parent: listView }
                        AnchorChanges {
                            target: content
                            anchors { horizontalCenter: undefined; verticalCenter: undefined }
                        }
                    }

                    RowLayout{
                        id:rowId
                        anchors.fill: parent
                        height: textAreaId.height
                        spacing: 6
                        CheckBox {
                            id: checkboxId
                            checked: object.done

                            onCheckStateChanged: {
                                object.done = checked
                            }
                        }

                        MouseArea{
                            width: textAreaId.width
                            height: textAreaId.height
                            Layout.alignment: Qt.AlignLeft
                            Layout.fillWidth: true

                            onDoubleClicked: {
                                var component = Qt.createComponent("qrc:/Window/TaskEditWindow.qml")
                                while (component.status !== Component.Ready) {
                                    console.log("component not ready");
                                }

                                var win = component.createObject(content,{"editIndex": index,"model":model});
                                win.show();
                                console.log("double clicked" + index);
                                }

                        TextArea {
                            id: textAreaId
                            property bool inEditMode: false

                            font.bold: true
                            font.strikeout: checkboxId.checked
                            text: object.title
                            readOnly: !inEditMode
                            wrapMode: TextEdit.WordWrap


                            onEditingFinished: {
                                inEditMode = false
                                object.title = text
                                Qt.inputMethod.hide()
                            }
                        }

                        }

                    Button {
                        id: deleteTaskButtonId
                        text: qsTr("删除")
                        visible: !object.done
                        Layout.alignment: Qt.AlignRight
                        background: Rectangle {
                                  anchors.fill: parent
                                  color: deleteTaskButtonId.down ? "#99eeeeee" : "transparent"
                              }
                        onReleased: {
                            backend.deleteCompletedTask(object)
                        }
                    }

                    Text {
                        id: dragIndicatorTextId
                        text: qsTr("拖拽")
                        Layout.alignment: Qt.AlignRight
                        width: 40
                        height: 40

                        Behavior on color { ColorAnimation { duration: 100 } }
                    }
                }
               }
            }
        }

